//
//  BVShoppingCartFrame.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/27.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVShoppingCartFrame.h"
#define LARGE_SPACE 10
#define SMALL_SPACE 5

@interface BVShoppingCartFrame ()

@end

@implementation BVShoppingCartFrame

- (void)setShoppingCart:(BVShoppingCart *)shoppingCart {
    _shoppingCart = shoppingCart;
    
    [self setupChildViewFrame];
}

- (void)setupChildViewFrame {
    
    // 单选
    float radioButtonX = LARGE_SPACE;
    float radioButtonY = 30;
    float radioButtonW = 30;
    float radioButtonH = 50;
    self.radioButtonFrame = CGRectMake(radioButtonX, radioButtonY, radioButtonW, radioButtonH);
    
    // 配图
    float imgViewX = CGRectGetMaxX(self.radioButtonFrame) + LARGE_SPACE;
    float imgViewY = LARGE_SPACE;
    float imgViewW = 80;
    float imgViewH = imgViewW;
    self.imgViewFrame = CGRectMake(imgViewX, imgViewY, imgViewW, imgViewH);
    
    // 标题
    CGRect nameRect = [self.shoppingCart.title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15]} context:nil];
    
    float titleX = CGRectGetMaxX(self.imgViewFrame) + LARGE_SPACE;
    float titleY = imgViewY;
    float titleW = nameRect.size.width;
    float titleH = nameRect.size.height;
    self.titleLabelFrame = CGRectMake(titleX, titleY, titleW, titleH);
    
    // 促销
    float salesX = CGRectGetMaxX(self.titleLabelFrame) + SMALL_SPACE;
    float salesY = titleY;
    float salesW = 35;
    float salesH = 18;
    self.salesLabelFrame = CGRectMake(salesX, salesY, salesW, salesH);
    
    // 单价
    CGRect unitPriceRect = [[NSString stringWithFormat:@"%@元/%@",self.shoppingCart.unit_price,self.shoppingCart.unit] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    float unitPriceX = titleX;
    float unitPriceY = CGRectGetMaxY(self.titleLabelFrame) + LARGE_SPACE;
    float unitPriceW = unitPriceRect.size.width;
    float unitPriceH = unitPriceRect.size.height;
    self.unitPriceLabelFrame = CGRectMake(unitPriceX, unitPriceY, unitPriceW+3, unitPriceH);
    
    // 重量
    CGRect weightRect = [[NSString stringWithFormat:@"%@%@装",self.shoppingCart.weight,self.shoppingCart.unit] boundingRectWithSize:CGSizeMake(MAXFLOAT, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    float weightX = CGRectGetMaxX(self.unitPriceLabelFrame) + SMALL_SPACE;
    float weightY = unitPriceY;
    float weightW = weightRect.size.width;
    float weightH = weightRect.size.height;
    self.weightLabelFrame = CGRectMake(weightX, weightY, weightW+3, weightH);
    
    // 合计
    float allPriceX = titleX;
    float allPriceY = CGRectGetMaxY(self.unitPriceLabelFrame) + LARGE_SPACE;
    float allPriceW = LARGE_SPACE*8;
    float allPriceH = LARGE_SPACE*2;
    self.allPriceLabelFrame = CGRectMake(allPriceX, allPriceY, allPriceW, allPriceH);
    
    // 限购
    CGRect limitRect = [self.shoppingCart.notice boundingRectWithSize:CGSizeMake(ScreenW-SMALL_SPACE*1.5-imgViewX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    float noticeX = imgViewX;
    float noticeY = CGRectGetMaxY(self.imgViewFrame) + LARGE_SPACE;
    float noticeW = limitRect.size.width;
    float noticeH = limitRect.size.height + SMALL_SPACE;
    self.noticeLabelFrame = CGRectMake(noticeX, noticeY, noticeW, noticeH);
    
    float buttonWH = LARGE_SPACE*2.5;
    // 加
    float addButtonW = buttonWH;
    float addButtonH = addButtonW;
    float addButtonX = ScreenW - addButtonW - SMALL_SPACE*3;
    float addButtonY = allPriceY;
    self.addButtonFrame = CGRectMake(addButtonX, addButtonY, addButtonW, addButtonH);
    
    // 数量
    float numLabelW = addButtonW + LARGE_SPACE;
    float numLabelH = addButtonH;
    float numLabelX = addButtonX - numLabelW;
    float numLabelY = addButtonY;
    self.goodsNumLabelFrame = CGRectMake(numLabelX, numLabelY, numLabelW, numLabelH);
    
    // 减
    float minusButtonW = addButtonW;
    float minusButtonH = addButtonH;
    float minusButtonX = numLabelX - minusButtonW;
    float minusButtonY = addButtonY;
    self.minusButtonFrame = CGRectMake(minusButtonX, minusButtonY, minusButtonW, minusButtonH);
    
    // 删除
    float deleteButtonW = buttonWH*.7;
    float deleteButtonH = buttonWH*.7;
    float deleteButtonX = ScreenW-deleteButtonW-10;
    float deleteButtonY = LARGE_SPACE;
    self.deleteButtonFrame = CGRectMake(deleteButtonX, deleteButtonY, deleteButtonW, deleteButtonH);
    
    // cell的高度
    if (self.shoppingCart.notice.length == 0) {
        self.cellHeight = CGRectGetMaxY(self.imgViewFrame) + LARGE_SPACE;
    } else {
        self.cellHeight = CGRectGetMaxY(self.noticeLabelFrame) + LARGE_SPACE;
    }
}

@end
